package dao;

import entity.Route;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DBUtil;

import java.sql.SQLException;
import java.util.List;

public class RouteDao {
    QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    //可以将下划线命名的改为驼峰命名
    BeanListHandler<Route> blh = new BeanListHandler<>(
            Route.class, new BasicRowProcessor(new GenerousBeanProcessor()));

    /**
     * 添加路线
     */
    public int add(Route route){
        String sql = "insert into `route` values (?,?,?,?,?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql, route.getId(),route.getTitle(),route.getDeparture(),route.getDestination(),
                    route.getDepartureDate(), route.getEndDate(),route.getPrice(),route.getPictureUrl(),
                    route.getIntroduce(),route.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 单表查询
     */
    public List<Route> query(String sql, Object... params){
        try {
            return queryRunner.query(sql, blh, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
